package com.sam.im.heiqun.uis.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sam.im.heiqun.R;
import com.sam.im.heiqun.utils.ToolsUtils;
import com.yuyh.library.uis.activitys.BaseSwipeBackActivity;
import com.yuyh.library.utils.EventBusType;

import org.greenrobot.eventbus.EventBus;

import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wangguodong on 2017/9/7.
 */

public class MoreLanguageSetActivity extends BaseSwipeBackActivity {

//ok
    @BindView(R.id.ok)
    TextView ok;
    @BindView(R.id.radio_zh_cn)
    RadioButton radio_zh_cn;
    @BindView(R.id.radio_zh_tw)
    RadioButton radio_zh_tw;
    @BindView(R.id.radio_en)
    RadioButton radio_en;
    @BindView(R.id.radio_jp)
    RadioButton radio_jp;
    @BindView(R.id.radio_ko)
    RadioButton radio_ko;

    public static void start(Activity activity ){
        Intent intent = new Intent(activity, MoreLanguageSetActivity.class);
        activity.startActivity(intent);
    }

    @Override
    protected String getTitleText() {
        return getResources().getString(R.string.more_lan);
    }

    @Override
    protected void initViews(Bundle savedInstanceState) {

        ok.setText(getResources().getString(R.string.ok));
        ok.setVisibility(View.VISIBLE);

        String language = ToolsUtils.saveLanguage(MoreLanguageSetActivity.this,"",2);
        if ("1".equals(language)){
            radio_zh_cn.setChecked(true);
        }else if ("2".equals(language)){
            radio_zh_tw.setChecked(true);
        }else if ("3".equals(language)){
            radio_en.setChecked(true);
        }else if ("4".equals(language)){
            radio_jp.setChecked(true);
        }else if ("5".equals(language)){
            radio_ko.setChecked(true);
        }else {
            radio_zh_cn.setChecked(true);
        }

    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_more_language_set;
    }


    @OnClick({R.id.ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ok:
                String language = "1";
                Locale myLocale = new Locale(Locale.getDefault().getLanguage());
                if (radio_zh_cn.isChecked()){
                    language = "1" ;
                    myLocale = Locale.SIMPLIFIED_CHINESE;
                }
                if (radio_zh_tw.isChecked()){
                    language = "2" ;
                    myLocale = Locale.TRADITIONAL_CHINESE;
                }
                if (radio_en.isChecked()){
                    language = "3" ;//ok
                    myLocale = Locale.ENGLISH ;
                }
                if (radio_jp.isChecked()){
                    language =  "4" ;
                    myLocale = Locale.JAPANESE;
                }
                if (radio_ko.isChecked()){
                    language = "5" ;//ok
                    myLocale = Locale.KOREAN ;
                }
//                Log.i("chatbase", "onClick: ============language="+language);

                Resources res = getResources();
                DisplayMetrics dm = res.getDisplayMetrics();
                Configuration conf = res.getConfiguration();
                conf.locale = myLocale;
                res.updateConfiguration(conf, dm);

                ToolsUtils.saveLanguage(MoreLanguageSetActivity.this,language,1);
                EventBus.getDefault().post(EventBusType.REFRESH_BASE_ACTIVITY);
                showToast(getResources().getString(R.string.set_success));
//                Intent intent = new Intent(this, MainActivity.class);
//                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//                startActivity(intent);
//                // 杀掉进程
//                android.os.Process.killProcess(android.os.Process.myPid());
//                System.exit(0);
                scrollToFinishActivity();
                break;
            case R.id.pre_v_back:
                scrollToFinishActivity();
                break;
        }
    }

}
